package com.chezhujie.www.service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Tuserrole;
import com.chezhujie.www.persistence.TuserroleMapper;
import com.chezhujie.www.utils.UUIDUtils;



@Service("tuserroleService")
public class TuserroleService {
	@Autowired
	private TuserroleMapper tuserroleMapper;

	/**
	 * 查询某个用户角色表
	 * @param param
	 * @return
	 */
	public Tuserrole getTuserrole(Map<String,Object> param) {
		return tuserroleMapper.getTuserrole(param);
	}
   
	/**
	 * 查询用户角色表列表
	 * @return
	 */
	public Map<String,Object> getTuserroleList(Map<String,Object> param) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("total", tuserroleMapper.getTotalRows(param));
		returnMap.put("rows", tuserroleMapper.getTuserroleList(param));
		return returnMap;
	}
   
	/**
	 * 查询用户角色表列表
	 * @return
	 */
	public  Pagination getTuserroleList(Map<String,Object> param,int pageSize,Integer pageNo,String order,String sort) {
		Pagination p = new Pagination();
		p.setPageNo(pageNo);
		p.setPageSize(pageSize);
		p.setOrder(order);
		p.setSort(sort);
		param.putAll(p.toMap());
		int count=tuserroleMapper.getTotalRows(param);
		List<Tuserrole> list=	tuserroleMapper.getTuserroleList(param);
	
		p.setList(list);
		p.setTotalCount(count);
		return p;
		
	}
      
	/**
	 * 新增用户角色表
	 * @param tuserrole
	 * @param param
	 */
	@Transactional
	public void insertTuserrole(Tuserrole tuserrole) {
		if(StringUtils.isBlank(tuserrole.getId())){
			tuserrole.setId(UUIDUtils.getUUID());
		}
		tuserroleMapper.insertTuserrole(tuserrole);
	}
   
	/**
	 * 编辑用户角色表
	 * @param tuserrole
	 * @param param
	 */
	@Transactional
	public void updateTuserrole(Tuserrole tuserrole) {
		tuserroleMapper.updateTuserrole(tuserrole);
	}
   
	/**
	 * 删除用户角色表
	 * @param param
	 */    
	@Transactional
	public void deleteTuserrole(List<String> roleidList) {
		for(String roleid: roleidList){
	  		Map<String,Object> param = new HashMap<String,Object>();
	  		param.put("roleid", roleid);
	 	   tuserroleMapper.deleteTuserrole(param);	  		
		}
	}

	public List<String> findTrolesByuserId(String userid) {
		
		return tuserroleMapper.findTrolesByuserId(userid);
	}
	
	public List<String> findBussinessByuserId(String userid) {
		
		return tuserroleMapper.findBussinessByuserId(userid);
	}	

	public void deleteTuserroleByUserid(String userid) {
		tuserroleMapper.deleteTuserroleByUserid(userid);
	}

	public List<Tuserrole> findTroleByuserId(String userid) {
		return tuserroleMapper.findTroleByuserId(userid);
	}


}